#include <stdbool.h>

#include "TM4C123GH6PM.h"
#include "driverlib/timer.h"
#include "driverlib/uart.h"
#include "driverlib/interrupt.h"
#include "inc/hw_ints.h"

#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"

#include "myCAP/myCap.h"
#include "mySerial/mySerial.h"
#include "myTimer/myTimer.h"
#include "myPWM/myPWM.h"
#include "myIIC/myIIC.h"
#include "Filter/Filters.h"
#include "BEEP/beep.h"
#include "MOTOR/motor.h"
#include "LED/led.h"
#include "hc_sr04/hc_sr04.h"
#include "myOLED/myOLED.h"
#include "SG90/sg90.h"
/*
*************************** 中断优先表(数字越小越优先) *******************************
0:GPIOE_Handler                       2:TIMER0A_Handler       4:TIMER5A_Handler
1:GPIOF_Handler                       3:UART0_Handler | UART2_Handler
*/

/*
*************************** 任务优先表(数字越大越优先) *******************************
3:LED_Task  Ranging_Task  beHit_task      5:motorControl_Task
4:serial_handle_Task                      6: OLEDShowing_Task
*/

/**************************** 全局变量定义区 ********************************/


uint8_t beHit_Flag = 0;
void beHit_task(void *pvParameters)
{
  // uint8_t data_buf[8] = {'R', 'T', 'S', 0, 30, 10, 50, 'E'}; // car1
  uint8_t data_buf[8] = {'R', 'T', 'S', 0, 20, 25, 30, 'E'}; // car2
  uint32_t data_len = 8;
  vTaskDelay(3000 / portTICK_RATE_MS);
  UART_send_bytes(UART2_BASE, data_buf, data_len);
  while (1)
  {
    if(beHit_Flag){
      beHit_Flag = 0;
      data_buf[3] = 0x01;
      UART_send_bytes(UART2_BASE, data_buf, data_len);
      sg90_test();
    }
    else{
      vTaskDelay(5/portTICK_PERIOD_MS);
    }
  }
}

void beHitComponent_init(void)
{
  /* 配置 TIMER1A */
  SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1);
  TimerConfigure(TIMER1_BASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_PERIODIC_UP); // 配置定时器，将定时器拆分，并配置拆分后的定时器A为周期性计数
  TimerLoadSet(TIMER1_BASE, TIMER_A, 80);                                      // 设置定时器装载值
  TimerMatchSet(TIMER1_BASE, TIMER_A, 80);
  TimerDisable(TIMER1_BASE, TIMER_A);                                          // 失能定时器

  /* 配置 PE4 的外部中断 */
  SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
  GPIODirModeSet(GPIOE_BASE, GPIO_PIN_4, GPIO_DIR_MODE_IN);
  GPIOPadConfigSet(GPIOE_BASE, GPIO_PIN_4, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPD);
  GPIOIntTypeSet(GPIOE_BASE, GPIO_PIN_4, GPIO_BOTH_EDGES); // 上升沿触发
  IntPrioritySet(INT_GPIOE, 0 << 5);
  GPIOIntEnable(GPIOE_BASE, GPIO_INT_PIN_4);
  IntEnable(INT_GPIOE);
  xTaskCreate(beHit_task, "beHit_task", 128, NULL, 3, NULL); /* 创建 test_Task 任务 */
}


void AppCreate_Task(void *pvParameters)
{
  taskENTER_CRITICAL(); // 进入临界区
  serial_init();
  printf("\r\n[I]Program is Run-begining\r\n[I]The system clock is [%d]\r\n", SysCtlClockGet());
  LED_Init();
  BEEP_Init();
  HC_SR04_Init();
  Motor_Init(); // 初始化电机驱动
  beHitComponent_init();
  sg90_driver_init();
  OLED_init();
  printf("=====准备进入FreeRTOS!=====\r\n");
  vTaskDelete(NULL);   // 删除 AppCreate 任务
  taskEXIT_CRITICAL(); // 退出临界区
}

int main(void)
{
  SysCtlClockSet(SYSCTL_SYSDIV_2_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); // 设置系统时钟为80MHz
  xTaskCreate(AppCreate_Task, "AppCreate_Task", 128, NULL, 3, NULL);                        /* 创建 AppTaskCreate 任务 */
  vTaskStartScheduler(); /* 开启调度器 */
  return 0;
}


// GPIOE4外部中断函数
void GPIOE_Handler(void){
  GPIOIntClear(GPIOE_BASE, GPIOIntStatus(GPIOE_BASE, true));
  static time_t measureTime;
  if(GPIOPinRead(GPIOE_BASE, GPIO_PIN_4) == GPIO_PIN_4){
    // 被拉高
    measureTime = getCPURunTime();
  }
  else{
    if (getCPURunTime()-measureTime >= 5)
    {
      // 被红外攻击了
      beHit_Flag = 1;
    }
    else{
      printf("[E] Error IRNEC");
    }

  }
}


